package com.maimang.platform.dsp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maimang.platform.dsp.constant.AdvertiserApi;
import com.maimang.platform.dsp.constant.ParamFields;
import com.maimang.platform.dsp.entity.Ad;
import com.maimang.platform.dsp.entity.AdvertiserIds;
import com.maimang.platform.dsp.mapper.AdMapper;
import com.maimang.platform.dsp.service.AccessTokenService;
import com.maimang.platform.dsp.service.AdService;
import com.maimang.platform.dsp.service.AdvertiserIdsService;
import com.maimang.platform.dsp.utils.ApiService;
import com.maimang.platform.lang.ConvertUtils;
import com.maimang.platform.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname AdServiceImpl
 * @description <br>广告计划服务层实现类</br>
 * @date 2019-11-25 11:13:45
 * @email jintaozhao@qq.com
 */
@Service
public class AdServiceImpl implements AdService {

	@Autowired
	private AdMapper adMapper;

	@Autowired
	private ApiService apiService;

	@Autowired
	private AdvertiserIdsService advertiserIdsService;

	@Autowired
	private AccessTokenService accessTokenService;

	/**
	 * 查询广告计划信息
	 *
	 * @param id 广告计划ID
	 * @return 广告计划信息
	 */
	@Override
	public Ad selectAdById(String id) {
		return adMapper.selectAdById(id);
	}

	/**
	 * 查询广告计划列表
	 *
	 * @param ad 广告计划信息
	 * @return 广告计划集合
	 */
	@Override
	public List<Ad> selectAdList(Ad ad) {
		return adMapper.selectAdList(ad);
	}

	/**
	 * 新增广告计划
	 *
	 * @param ad 广告计划信息
	 * @return 结果
	 */
	@Override
	public int insertAd(Ad ad) {
		return adMapper.insertAd(ad);
	}

	/**
	 * 修改广告计划
	 *
	 * @param ad 广告计划信息
	 * @return 结果
	 */
	@Override
	public int updateAd(Ad ad) {
		return adMapper.updateAd(ad);
	}

	/**
	 * 删除广告计划对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteAdByIds(String ids) {
		return adMapper.deleteAdByIds(ConvertUtils.toStrArray(ids));
	}

	/**
	 * 同步广告计划信息
	 *
	 * @param advertiserId 代理商广告主id
	 */
	@Override
	public void syncAd(String advertiserId) {
		List<AdvertiserIds> idsList = advertiserIdsService.selectAdvertiserIds(Long.valueOf(advertiserId));

		for (int i = 0; i < idsList.size(); i++) {
			AdvertiserIds advertiser = idsList.get(i);
			getAd(advertiser.getId(), 1, 20);
		}
	}

	/**
	 * 获取广告计划信息
	 *
	 * @param advertiserId 广告主id
	 * @param page         页数
	 * @param pageSize     每页尺寸
	 */
	@Override
	public void getAd(Long advertiserId, int page, int pageSize) {
		Map param = new HashMap();

		if (StringUtils.isNotNull(advertiserId)) {
			param.put("advertiser_id", advertiserId);
		}
		if (page >= 0) {
			param.put("page", page);
		}
		if (pageSize > 0) {
			param.put("page_size", pageSize);
		}
		param.put("fields", ParamFields.AD_INFO);

		String accessToken = accessTokenService.accessToken();

		JSONObject result = apiService.sendGet(AdvertiserApi.AD_GET, accessToken, param);
		if (result != null) {
			if (result.getInteger("code") == 0) {
				JSONObject data = result.getJSONObject("data");
				if (data != null) {
					JSONArray list = data.getJSONArray("list");
					List<Ad> adList = list.toJavaList(Ad.class);
					if (adList.size() > 0) {
						for (int i = 0; i < adList.size(); i++) {
							Ad ad = adList.get(i);
							if (StringUtils.isNotEmpty(ad.getId())) {
								// 删除旧数据，减少查询次数
								adMapper.deleteAdById(ad.getId());
								// 重新插入新的数据
								adMapper.insertAd(ad);
							}
						}
					}
					JSONObject pageInfo = data.getJSONObject("page_info");
					if (StringUtils.isNotNull(pageInfo)) {
						int current_page = pageInfo.getIntValue("page");
						int page_size = pageInfo.getIntValue("page_size");
						int total_page = pageInfo.getIntValue("total_page");
						if (current_page < total_page) {
							try {
								Thread.sleep(1000);
								getAd(advertiserId, current_page + 1, page_size);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		}
	}
}
